package com.breadenglish.cms.api.app.sys.manager;

import com.breadenglish.cms.api.app.common.anotation.EnablePaging;
import com.breadenglish.cms.api.app.common.context.PagingContextHolder;
import com.breadenglish.cms.api.app.common.page.Page;
import com.breadenglish.cms.api.app.common.page.QueryPage;
import com.breadenglish.cms.api.app.common.utils.FileUtils;
import com.breadenglish.cms.api.app.common.utils.PropertyConfigurerUtils;
import com.breadenglish.cms.api.app.common.utils.StringUtils;
import com.breadenglish.cms.api.app.dao.WordDao;
import com.breadenglish.cms.api.app.dao.WordDetailDao;
import com.breadenglish.cms.api.app.domain.constants.Constants;
import com.breadenglish.cms.api.app.domain.constants.MessageConstants;
import com.breadenglish.cms.api.app.domain.db.Audio;
import com.breadenglish.cms.api.app.domain.db.Word;
import com.breadenglish.cms.api.app.domain.db.WordDetail;
import com.breadenglish.cms.api.app.domain.model.ResultData;
import com.breadenglish.cms.api.app.domain.model.ResultMessage;
import com.breadenglish.cms.api.app.domain.query.WordQuery;
import com.breadenglish.cms.api.app.sys.service.WordService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 作者: guo yao
 * 日期: 2015年12月28日
 * 时间: 下午13:28
 */
@Controller
@RequestMapping("/likeWord")
public class WordLikeController {

    public static String filePath = PropertyConfigurerUtils.getProperty(Constants.FILE_PATH);

    private final static Logger logger = LoggerFactory.getLogger(WordLikeController.class);

    @Resource
    private WordService wordService;

    /**
     * who be like主页面
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/forwardQueryLike.html")
    public String forwardQueryLike() throws Throwable {
        return "app/word_a_day/query_like_word";
    }

    /**
     * 添加每日一词页面
     *
     * @return
     * @throws Throwable
     */
    @RequestMapping(value = "/insertLikeTemplate.html")
    public String insertLikeTemplate() throws Throwable {
        return "app/word_a_day/insert_like_word";
    }


    /**
     * 查询所有Word
     *
     * @param queryPage
     * @param word
     * @return Map
     */
    @EnablePaging
    @RequestMapping(value = "/queryLikeWord.html")
    @ResponseBody
    public Map<String, Object> queryLikeWord(@ModelAttribute("queryPage") QueryPage queryPage, @ModelAttribute("word") Word word) {
        List<WordQuery> words = wordService.queryWord(word);
        // 获取翻页参数
        Page page = PagingContextHolder.getPage();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("total", page.getTotalResult());
        resultMap.put("rows", words);
        return resultMap;
    }

    /**
     * 查看word信息
     *
     * @param wordId
     * @return ModelAndView
     */
    @RequestMapping(value = "/displayWord.html")
    public ModelAndView displayWord(@RequestParam("wordId") String wordId) {
        ModelAndView mav = new ModelAndView();
//        Word word = wordService.selectByPrimaryKey(Long.valueOf(wordId));
        WordQuery wordQuery = wordService.queryWordById(Long.valueOf(wordId));
        mav.addObject("wordQuery", wordQuery);
        mav.setViewName("app/word_a_day/display_like_word");
        return mav;
    }

    /**
     * 增加WhoBeLike
     *
     * @param word
     * @return ResultData
     */
    @RequestMapping(value = "/insertLikeWord.html")
    @ResponseBody
    public ResultMessage insertLikeWord(@ModelAttribute("word") Word word, HttpServletRequest request) throws Throwable {
        String savePath = filePath;
        if (filePath.equals("/upload")) {
            savePath = request.getRealPath(filePath);
        }
        //创建文件
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
        savePath = savePath + File.separator + formater.format(new Date());
        FileUtils.createDir(savePath);
        wordService.insert(word, savePath);
        ResultMessage message = new ResultMessage();
        message.setTitle(PropertyConfigurerUtils.getProperty(MessageConstants.MESSAGE_SUCCESS_TITLE));
        message.setSuccess(true);
        message.getContent().add("添加每日一词成功");
        return message;
    }

    /**
     * 删除Word
     *
     * @param wordId
     * @return ResultData
     */
    @RequestMapping(value = "/deleteLikeWord.html")
    @ResponseBody
    public ResultMessage deleteLikeWord(@RequestParam("wordId") String wordId) throws Throwable {
        ResultData resultData = new ResultData();
        int result = wordService.deleteByPrimaryKey(wordId);
        ResultMessage message = new ResultMessage();
        message.setTitle(PropertyConfigurerUtils.getProperty(MessageConstants.MESSAGE_SUCCESS_TITLE));
        message.setSuccess(true);
        message.getContent().add("删除每日一词成功");
        return message;
    }

    /**
     * 跳转到更新页面
     *
     * @param wordId
     * @return ModelAndView
     */
    @RequestMapping(value = "/forwardUpdateWord.html")
    public ModelAndView forwardUpdateWord(@RequestParam("wordId") String wordId) {
        ModelAndView mav = new ModelAndView();
//        Word word = wordService.selectByPrimaryKey(Long.valueOf(wordId));
        WordQuery wordQuery = wordService.queryWordById(Long.valueOf(wordId));
        mav.addObject("wordQuery", wordQuery);
        mav.setViewName("app/word_a_day/edit_like_word");
        return mav;
    }

    /**
     * 更新 Word
     *
     * @param word
     * @return ResultData
     */
    @RequestMapping(value = "/updateLikeWord.html")
    @ResponseBody
    public ResultMessage updateLikeWord(@ModelAttribute("word") Word word, HttpServletRequest request) {
        String savePath = filePath;
        if (filePath.equals("/upload")) {
            savePath = request.getRealPath(filePath);
        }
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
        savePath = savePath + File.separator + formater.format(new Date());
        FileUtils.createDir(savePath);
        wordService.update(word, savePath);
        ResultMessage message = new ResultMessage();
        message.setTitle(PropertyConfigurerUtils.getProperty(MessageConstants.MESSAGE_SUCCESS_TITLE));
        message.setSuccess(true);
        message.getContent().add("更新每日一词成功");
        return message;
    }


    /**
     * 显示详细信息
     *
     * @param wordId
     * @return
     * @throws Throwable
     */
    @RequestMapping(value = "/showDetailWord.html")
    public ModelAndView showDetailWord(@RequestParam("wordId") Long wordId) throws Throwable{
        ModelAndView mav = new ModelAndView();
        WordQuery wordQuery = wordService.queryWordById(Long.valueOf(wordId));
        mav.addObject("detail",wordQuery);
        mav.setViewName("app/word_a_day/detail_word");
        return mav;
    }

}
